﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;

using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;

namespace WebSite.admin.order
{
    public partial class MeterialOrderAdd : BaseAdmin
    {
        // Fields

        DALProfessionAreaType theProfessionAreaType = new DALProfessionAreaType();
        Shoppro theShoppro = new Shoppro();

        // Methods

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
            this.GridViewBind();
        }

        void BindProfessionAreaType()
        {
            DataSet ds;
            ds = theProfessionAreaType.GetList(" 1= 1 ",null);
            this.ddlProfessionAreaType.DataTextField = "Name";
            this.ddlProfessionAreaType.DataValueField = "Id";
            this.ddlProfessionAreaType.DataSource = ds;
            this.ddlProfessionAreaType.DataBind();
            this.ddlProfessionAreaType.Items.Insert(0, new ListItem("所属专区", ""));

        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if ((e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.DataRow))
            {
                CheckBox box = new CheckBox();
                box = (CheckBox)e.Row.FindControl("CheckBox1");
                if (box != null)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=\"" + e.Row.Style["BACKGROUND-COLOR"] + "\"");
                        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor=\"#efefef\"");
                    }
                    else if (e.Row.RowType == DataControlRowType.Header)
                    {
                        box.Attributes.Add("onclick", "SelectAll(this.checked)");
                      
                    }
                  
                }
            }
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sqlsort = GetSql();

            this.GridViewBind();
        }

        string GetSql()
        {
            string sql = "select * from DB_Shoppro where visible=1 and proisshow=1 and proisnew2=1 and isnull(proku1,0) > 0";

            if (this.typeid.SelectedValue.ToString() != "0")
            {
                sql = sql + " and typeid in(" + re.returntype(this.typeid.SelectedValue.ToString(), this.typeid.SelectedValue.ToString(), info.languages("")) + ")";
            }
            if (this.propinpai.SelectedValue.ToString() != "0")
            {
                sql = sql + " and propinpai=" + this.propinpai.SelectedValue.ToString() + "";
            }
            if (re.replace(this.keyname.Value.ToString()) != "")
            {
                sql = sql + " and (proname like '%" + re.replace(this.keyname.Value.ToString()) + "%' or procode like '%" + re.replace(this.keyname.Value.ToString()) + "%')";
            }
            if (this.ddlProfessionAreaType.SelectedItem.Value != "")
            {
                sql = sql + " and ProfessionAreaTypeId = " + this.ddlProfessionAreaType.SelectedItem.Value;
            }

            return sql;
        }

        private void Bind()
        {
            string sql = "select * from DB_Pinpai where visible=1  order by pinpaiorder";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    this.propinpai.Items.Add(new ListItem(row["pinpainame"].ToString(), row["pinpaiid"].ToString()));
                }
            }
            sql = "select * from DB_Productclass where visible=1 and languages=" + info.languages("") + " and parentid=0 order by classorder";
            table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row2 in table.Rows)
                {
                    this.typeid.Items.Add(new ListItem(row2["classname"].ToString(), row2["classid"].ToString()));
                }
            }
        }

        private void GridViewBind()
        {
            string sql = GetSql();
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            this.GridView1.DataSource = table.DefaultView;
            this.GridView1.DataBind();
            this.LblCurrentIndex.Text = "当前第 " + ((this.GridView1.PageIndex + 1)).ToString() + "页";
            this.LblPageCount.Text = "共 " + this.GridView1.PageCount.ToString() + "页";
            this.LblRecordCount.Text = "总计 " + table.Rows.Count.ToString() + "个记录";
            if (table.Rows.Count == 0)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
                this.LblCurrentIndex.Visible = false;
                this.LblPageCount.Visible = false;
                this.LblRecordCount.Visible = false;
            }
            else if (this.GridView1.PageCount == 1)
            {
                this.btnFirst.Visible = false;
                this.btnPrev.Visible = false;
                this.btnNext.Visible = false;
                this.btnLast.Visible = false;
            }
            this.btnFirst.CommandName = "1";
            this.btnPrev.CommandName = (this.GridView1.PageIndex == 0) ? "1" : this.GridView1.PageIndex.ToString();
            this.btnNext.CommandName = (this.GridView1.PageCount == 1) ? this.GridView1.PageCount.ToString() : ((this.GridView1.PageIndex + 2)).ToString();
            this.btnLast.CommandName = this.GridView1.PageCount.ToString();
            this.ddlCurrentPage.Items.Clear();
            if (this.GridView1.PageCount > 0)
            {
                for (int i = 1; i <= this.GridView1.PageCount; i++)
                {
                    this.ddlCurrentPage.Items.Add(i.ToString());
                }
                this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            this.GridViewBind();
        }


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!base.IsPostBack)
            {
                if (base.CompanyId == null)
                {
                    base.ValidateLoginRule(ERule.MeterialOrderAdd);
                }
                this.BindProfessionAreaType();
                this.Bind();
                this.GridViewBind();  
            }
        }

        protected void PagerButtonClick(object sender, EventArgs e)
        {
            this.GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
            this.GridViewBind();
        }

        protected void btnNextStep_Click(object sender, EventArgs e)
        {
            string sIds = "";
            for (int i = 0; i <= (this.GridView1.Rows.Count - 1); i++)
            {
                CheckBox box = (CheckBox)this.GridView1.Rows[i].FindControl("CheckBox1");
                if (box.Checked)
                {
                    sIds = sIds + this.GridView1.DataKeys[i].Value + ",";
                }
            }
            if (sIds.Length > 0)
            {
                sIds = sIds.Remove(sIds.Length - 1);
                MeteriaOrderCardIds = sIds;
                Response.Redirect("MeterialOrderAdd_02.aspx");
            }

        }


    }
}